package com.neuedu.His02.controller.outpatientDoctorWorkStation;

import com.neuedu.His02.entity.*;
import com.neuedu.His02.entity.diagnosisTemplate.DiagnosisTemplate;
import com.neuedu.His02.entity.recordTemplate.RecordTemplate;
import com.neuedu.His02.service.outpatientDoctorWorkStationService.OutpatientDiagnosisServer;

import com.neuedu.His02.util.EnumCode;
import com.neuedu.His02.util.ResultUtil;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @description 门诊确诊控制类
 *
 */
@RestController
public class OutpatientDiagnosisController {
    @Resource
    private OutpatientDiagnosisServer service;

    /**
     * @return {@link ResultUtil}
     * @description 初始化待确诊的患者信息
     */
    @PostMapping("/outpatientDoctorWorkStation/outpatientDiagnosis/initPatients")
    public ResultUtil initPatients(@RequestBody User user) {
        System.out.println("初始化待确诊患者:"+user);
        List<Register> result=new ArrayList<>();
        Integer code= EnumCode.OK.getValue();
        String msg="初始化成功";
        try {
            //先初始Register
            List<Register> temp=service.initPatients(user);
            for (int i = 0; i <temp.size() ; i++) {
                // 再从上面的数据里找到在MedicalRecord表里存在的，返回
                if(service.recordIsExist(temp.get(i))!=null){
                    result.add(temp.get(i));
                }
            }
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="初始化失败";
        }
        return new ResultUtil(code,msg,result);
    }

    /**
     * @return {@link ResultUtil}
     * @description 根据姓名搜索患者
     */
    @RequestMapping("/outpatientDoctorWorkStation/outpatientDiagnosis/searchByName")
    public ResultUtil searchByName(Register user) {
        System.out.println("搜索的姓名:"+user);
        List<Register> result=new ArrayList<>();
        Integer code=EnumCode.OK.getValue();
        String msg="搜索成功";
        try {
            result=service.searchByName(user);
            System.out.println("搜索的结果:"+result);
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="搜索失败";
        }
        return new ResultUtil(code,msg,result);
    }

    /**
     * @return {@link ResultUtil}
     * @description 根据病历号搜索患者
     */
    @RequestMapping("/outpatientDoctorWorkStation/outpatientDiagnosis/searchByNumber")
    public ResultUtil searchByNumber(Register user) {
        System.out.println("搜索的病历号:"+user);
        List<Register> result=new ArrayList<>();
        Integer code=EnumCode.OK.getValue();
        String msg="搜索成功";
        try {
            result=service.searchByNumber(user);
            System.out.println("搜索的结果:"+result);
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="搜索失败";
        }
        return new ResultUtil(code,msg,result);
    }

    /**
     * @return {@link ResultUtil}
     * @description 初始化患者的门诊病历首页
     */
    @RequestMapping("/outpatientDoctorWorkStation/outpatientDiagnosis/initRecord")
    public ResultUtil initRecord(MedicalRecord user) {
        System.out.println("初始化的病历患者"+user);
        MedicalRecord result=new MedicalRecord();
        Integer code=EnumCode.OK.getValue();
        String msg="已加载门诊病历首页";
        try {
            result=service.getRecord(user);
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="加载门诊病历首页失败";
        }
        return new ResultUtil(code,msg,result);
    }

    /**
     * @return {@link ResultUtil}
     * @description 初始化患者的检查结果
     */
    @RequestMapping("/outpatientDoctorWorkStation/outpatientDiagnosis/initCheckResult")
    public ResultUtil initCheckResult(MedicalRecord user) {
        System.out.println("初始化患者的检查结果:"+user);
        List<CheckApply> result=new ArrayList<>();
        Integer code=EnumCode.OK.getValue();
        String msg="初始化成功";
        try{
            result=service.getCheckResult(user);
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="初始化成功";
        }
        return new ResultUtil(code,msg,result);
    }

    /**
     * @return {@link ResultUtil}
     * @description 初始化患者的处置结果
     */
    @RequestMapping("/outpatientDoctorWorkStation/outpatientDiagnosis/initDealResult")
    public ResultUtil initDealResult(MedicalRecord user) {
        System.out.println("初始化患者的处置结果:"+user);
        List<CheckApply> result=new ArrayList<>();
        Integer code=EnumCode.OK.getValue();
        String msg="初始化成功";
        try {
            result=service.getDealResult(user);
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="初始化成功";
        }
        return new ResultUtil(code,msg,result);
    }

    /**
     * @return {@link ResultUtil}
     * @description 生成诊断模板(个人)
     */
    @PostMapping("/outpatientDoctorWorkStation/outpatientDiagnosis/getDiagnosisTemplatePersonally")
    public ResultUtil getRecordTemplatePersonally(@RequestBody User user) {
        System.out.println("生成个人病历模板:"+user);
        List<DiagnosisTemplate> result=new ArrayList<>();
        Integer code=EnumCode.OK.getValue();
        String msg="生成成功";
        try {
            result=service.getRecordTemplatePersonally(user);
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="生成失败";
        }
        return new ResultUtil(code,msg,result);
    }

    /**
     * @return {@link ResultUtil}
     * @description 生成诊断模板(科室)
     */
    @PostMapping("/outpatientDoctorWorkStation/outpatientDiagnosis/getDiagnosisTemplateByDepartment")
    public ResultUtil getRecordTemplateByDepartment(@RequestBody User user) {
        System.out.println("生成科室诊断模板:"+user);
        List<DiagnosisTemplate> result=new ArrayList<>();
        Integer code=EnumCode.OK.getValue();
        String msg="生成成功";
        try {
            result=service.getRecordTemplateByDepartment(user);
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="生成失败";
        }
        return new ResultUtil(code,msg,result);
    }

    /**
     * @return {@link ResultUtil}
     * @description 生成诊断模板(全院)
     */
    @PostMapping("/outpatientDoctorWorkStation/outpatientDiagnosis/getDiagnosisTemplateByHospital")
    public ResultUtil getRecordTemplateByHospital() {
        List<DiagnosisTemplate> result=new ArrayList<>();
        Integer code=EnumCode.OK.getValue();
        String msg="生成全院诊断模板成功";
        try {
            result=service.getRecordTemplateByHospital();
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="生成失败";
        }
        return new ResultUtil(code,msg,result);
    }

    /**
     * @return {@link ResultUtil}
     * @description 删除诊断模板
     */
    @PostMapping("/outpatientDoctorWorkStation/outpatientDiagnosis/deleteDiagnosisTemplate")
    public ResultUtil deleteRecordTemplate(@RequestBody DiagnosisTemplate diagnosisTemplate) {
        System.out.println("即将删除的诊断模板:"+diagnosisTemplate);
        Integer code=EnumCode.OK.getValue();
        String msg="删除成功";
        try {
            service.deleteRecordTemplate(diagnosisTemplate);
        }catch (Exception e){
            code=EnumCode.INTERNAL_SERVER_ERROR.getValue();
            msg="删除失败";
        }
        return new ResultUtil(code,msg);
    }

    /**
     * @return {@link ResultUtil}
     * @description 添加诊断模板
     */
    @RequestMapping("/outpatientDoctorWorkStation/outpatientDiagnosis/commitTemplate")
    public ResultUtil commitTemplate(DiagnosisTemplate diagnosisTemplate) {

        return null;
    }

    /**
     * @return {@link ResultUtil}
     * @description 确诊患者并更新病历信息
     */
    @RequestMapping("/outpatientDoctorWorkStation/outpatientDiagnosis/confirm")
    public ResultUtil confirm(MedicalRecord medicalRecord) {
        return null;
    }

    /**
     * @return {@link ResultUtil}
     * @description 初始化疾病种类
     */
    @PostMapping("/outpatientDoctorWorkStation/outpatientDiagnosis/diseaseCategory")
    public ResultUtil diseaseCategory() {
        return null;
    }

    /**
     * @return {@link ResultUtil}
     * @description 初始化疾病名称的前置函数
     */
    @PostMapping("/outpatientDoctorWorkStation/outpatientDiagnosis/initDiseasePrepare")
    public ResultUtil initDiseasePrepare() {
        return null;
    }

    /**
     * @return {@link ResultUtil}
     * @description 初始化疾病种类的前置函数
     */
    @PostMapping("/outpatientDoctorWorkStation/outpatientDiagnosis/initCategoryDiseasePrepare")
    public ResultUtil initCategoryDiseasePrepare() {
        return null;
    }
}
